package com.android.driveuploader;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

import java.io.IOException;

/**
 * Created by srikanthg on 9/1/14.
 */
public class OpenDriveFolderList extends ActionBarActivity {

    static final int DRIVE_FOLDER_PICKED = 44;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_my);
        setContentView(R.layout.activy_folders_list);

        if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) != ConnectionResult.SUCCESS) {
            Toast.makeText(this, getString(R.string.gms_connection_failed), Toast.LENGTH_LONG).show();
            return;
        }
        if (MyActivity.open_accountName == null) {
            Intent chooseAccountIntent = AccountPicker.newChooseAccountIntent(
                    null, // selectedAccount
                    null, // allowableAccounts
                    new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, // allowableAccountTypes
                    false, // alwaysPromptForAccount
                    null, // descriptionOverrideText
                    MyActivity.AUTHTOKENSCOPE_DRIVE, // addAccountAuthTokenType
                    null, // addAccountRequiredFeatures
                    null // addAccountOptions
            );
            startActivityForResult(chooseAccountIntent, 12);
        } else {
            Intent pickFolder = new Intent(this, PickFolderWithOpenerActivity.class);
            startActivity(pickFolder);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 12) {
            if (resultCode == RESULT_OK) {
                MyActivity.open_accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                startAuthenticatedUpload(MyActivity.open_accountName);
            }else {
                Toast.makeText(this, "Problem picking account", Toast.LENGTH_LONG).show();
            }
        }
        if( requestCode == DRIVE_FOLDER_PICKED) {
            Log.i("Yo!!", "Yo..Yo..");
        }
    }

    /** Starts an AsyncTask to perform the upload on a background thread. */
    private void startAuthenticatedUpload(final String accountName) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                doAuthenticatedUpload(accountName);
                return null;
            }

            private void doAuthenticatedUpload(String accountName) {
                try {
                    MyActivity.token = GoogleAuthUtil.getToken(OpenDriveFolderList.this,
                            accountName, "oauth2:" + MyActivity.AUTHTOKENSCOPE_DRIVE);
                    Log.i("token = ", MyActivity.token);
                } catch (UserRecoverableAuthException userAuthEx) {
                    // This is an authentication error that we can recover from.
                    return;
                } catch (GoogleAuthException e) {
                    Log.e("@@@", "GoogleAuthException", e);
                    return;
                } catch (IOException e) {
                    Log.e("###", "IOException", e);
                    return;
                }

            }
        }.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            Intent googlePS = new Intent(this, GooglePlayServicesActivity.class);
            startActivityForResult(googlePS, 4);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}